package com.dxy.md.user.controller;

import com.dxy.md.user.entity.User;
import com.dxy.md.user.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    private UserService userService;

    @PostMapping("/save")
    public User save(String name){
        Assert.notNull(name, "name 不能为空");
        User user = new User().setName(name);
        userService.saveUser(user);
        return user;
    }

    @GetMapping("/findById")
    public User findById(Long id){
        return userService.findById(id);
    }


    @GetMapping("/delete")
    public String delete(Long id){
        Assert.notNull(id, "id 不能为空");
        userService.deleteUser(id);
        return "OK";
    }

    @GetMapping("/testTran")
    public String testTran(){
        userService.testTransaction();
        return "OK";
    }

}
